<template>
  <div class="login-page">
    <!-- 标题 -->
    <van-nav-bar title="面经登录" />
    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密 码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <!-- 超链接 -->
    <router-link to="/register" class="link">还没有账号，去注册</router-link>
  </div>
</template>

<script>
// 按需导入需要的方法
import { loginAPI } from '@/api/user'

export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    // 点击提交，会执行这个方法。values表示输入框的数据
    async onSubmit (values) {
      // console.log('submit', values)
      const { data: res } = await loginAPI(values)
      // console.log(res)
      // 登录成功 提示
      this.$toast.success('登录成功')
      // 存token
      localStorage.setItem('mobile-token', res.data.token)
      // 跳转
      this.$router.push('/article')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #1989fa;
  font-size: 12px;
  float: right;
  margin-right: 20px;
}
</style>
